package com.boot.mgt.controller.web

import com.boot.core.result.ResultTo
import com.boot.core.util.ManagerKit
import com.boot.mgt.service.RoadRepairService
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*
import com.boot.mgt.model.TaRoadRepair
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*
import javax.servlet.http.HttpServletRequest





/**
 * 路段隐患治理controller
 * xiaying
 */
@RestController
@RequestMapping("/admin/roadManager")
class RoadRepairController {

    @Autowired
    private lateinit var roadRepairService: RoadRepairService

    /**
     * 获取事故信息列表
     */
    @GetMapping("/list")
    fun list(request: HttpServletRequest,roadName:String?,createTime_begin:String?, createTime_end:String?): ResultTo {
        return roadRepairService.list(roadName,createTime_begin,createTime_end)
    }

    /**
     * 获取事故信息列表
     */
    @GetMapping("/listRpc")
    fun listRpc(map: HashMap<String, Any>,roadName:String?,createTime_begin:String?, createTime_end:String?): ResultTo {
        return roadRepairService.list(roadName,createTime_begin,createTime_end)
    }

    companion object {
        private val log: Logger = LoggerFactory.getLogger(this::class.java)
    }

    /**
     * 新增路段信息
     */
    @PostMapping("/add")
    fun add(@RequestBody taRoadRepair: TaRoadRepair): ResultTo {
        taRoadRepair.createTime= SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(Date())
        taRoadRepair.editPolice=taRoadRepair.police
        taRoadRepair.editTime= SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(Date())
        if(taRoadRepair.status.equals(""))
        {
        taRoadRepair.status="未处理"
        }
        return roadRepairService.addRoad(taRoadRepair)


    }
    @GetMapping("/detail")
    fun detail(request: HttpServletRequest,roadId:Int): ResultTo {
         /*var roadIdTemp=0
          if(roadId!=null&&!roadId.trim().equals("")){
              roadIdTemp=Integer.parseInt(roadId)

          }*/
        return roadRepairService.detail(roadId)

    }
    /**
     * 编辑路段信息
     */
    @PostMapping("/editRoad")
    fun edit(request: HttpServletRequest,roadId:Int,policeNum:String?,roadName:String?,problem:String?,measures:String?,longitude:String?,latitude:String?,status:String?): ResultTo {
       var  taRoadRepair=TaRoadRepair()
        taRoadRepair.id=roadId
        taRoadRepair.road=roadName
        taRoadRepair.problem=problem
        taRoadRepair.measures=measures
        taRoadRepair.latitude=latitude
        taRoadRepair.longitude=longitude
        taRoadRepair.editPolice=policeNum
        taRoadRepair.status=status
        taRoadRepair.editTime=SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(Date())
        return roadRepairService.editRoadXia(taRoadRepair)
    }
    /**
     * 编辑路段信息
     */
    @PostMapping("/edit")
    fun edit(@RequestBody taRoadRepair: TaRoadRepair): ResultTo {
        taRoadRepair.editTime= SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(Date())
        taRoadRepair.status="未处理"
        taRoadRepair.editPolice = taRoadRepair.police;
        return roadRepairService.editRoad(taRoadRepair)
    }
    @GetMapping("/delete")
    fun delete(roadId: Int): ResultTo {
        return  roadRepairService.delete(roadId)
    }
    /**
     * 上传照片
     * policeNum:警员编号
     * roadId：路段id
     * imgList：Array<String> 照片base64数组
     */
    @PostMapping("/uploadImg")
    fun uploadImg(request: HttpServletRequest,policeNum:String,roadId:Int,imgList: Array<String> ): ResultTo {

        return roadRepairService.uploadImg(policeNum,roadId,imgList)
    }
    @PostMapping("/uploadOneImg")
    fun uploadOneImg(@RequestBody  request:Map<String, String>): ResultTo {
        var policeNum = request.get("policeNum")
        var roadId = request.get("roadId")
        var img = request.get("img")
        return roadRepairService.uploadOneImg(policeNum!!, roadId?.toInt()!!,img!!)
    }
    @PostMapping("/uploadOneImgRpc")
    fun uploadOneImgRpc(policeNum:String, roadId:Int, img: String): ResultTo {
        return roadRepairService.uploadOneImg(policeNum, roadId, img)
    }
    @GetMapping("/getImgList")
    fun  getImgList(request: HttpServletRequest, page_index: Int?, page_size:Int?, roadId: Int): ResultTo {

        return roadRepairService.getImgList(page_index?:1, page_size?:5,roadId)
    }

    @GetMapping("/delImg")
    fun delImg(id: Int): ResultTo {
        return  roadRepairService.delImg(id)
    }
}
